<?php
/**
 * Created by PhpStorm.
 * User: 牛赞赞
 * Date: 2021/1/13
 * Time: 14:25
 */

namespace App\Model;


use Hyperf\DbConnection\Db;

class UserScoreLog extends Model
{
    protected $table = 'user_score_log';
    protected $fillable = ['user_id', 'score', 'before', 'after', 'memo', 'createtime', 'symbol', 'game_id'];
    public $timestamps = false;

    public static function insertLog($arr)
    {
        try {
            self::query()->insert($arr);
            return ['code' => 0, 'msg' => '添加成功'];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    public static function totalWinGroup($date_between)
    {
        try {
            return $res = self::query()->where([
                ['createtime', '>=', $date_between[0]],
                ['createtime', '<=', $date_between[1]],
                ['symbol', '=', '+'],
                ['memo', '=', '麻将'],
                ['game_id', '=', 6]])
                ->select('user_id as account', Db::raw('SUM(score) as score'))
                ->groupBy('user_id')->get()->toArray();
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    public static function totalLoseGroup($date_between)
    {
        try {
            return $res = self::query()->where([
                ['createtime', '>=', $date_between[0]],
                ['createtime', '<=', $date_between[1]],
                ['symbol', '=', '-'],
                ['memo', '=', '麻将游戏'],
                ['game_id', '=', 6]])
                ->select('user_id as account', Db::raw('SUM(score) as score'))
                ->groupBy('user_id')->get()->toArray();
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }
}